package com.example.note_project.ui.activity;

import android.content.Intent;
import android.os.Bundle;

import android.widget.TextView;
import com.example.note_project.R;
import com.github.clans.fab.FloatingActionMenu;

import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,View.OnClickListener{

  private AppBarConfiguration mAppBarConfiguration;
  private Toolbar mToolbar;
  private FloatingActionMenu mFabMenu;
    String username=null;
//向右划出菜单，不会调用任何函数，因此菜单中控件的点击事件写在onSupportNavigateUp函数时不会被调用
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Toolbar toolbar = findViewById(R.id.toolbar);
      mToolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(mToolbar);
      initFabMenu();
        //FloatingActionButton mFabMenu = findViewById(R.id.floating_action_menu);


        //DrawerLayout布局
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        //获取用户名
        Intent intent=getIntent();
        username =intent.getStringExtra("username");
        NavigationView navigationView = findViewById(R.id.nav_view);
        View headerView = navigationView.getHeaderView( 0 );
        TextView textview = (TextView)headerView.findViewById(R.id.usernameId);
        textview.setText( username);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        //左侧侧滑菜单的所有id放入DrawerLayout
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send).setDrawerLayout(drawer).build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        //左侧侧滑菜单显示
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        //左侧侧滑菜单点击的 跳转
        NavigationUI.setupWithNavController(navigationView, navController);
    }
    //创建菜单，右上角设置按钮
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
  public void initFabMenu() {
      //功能按钮
    mFabMenu = findViewById(R.id.floating_action_menu);
    // 拍照 语音 手写 图为
    com.github.clans.fab.FloatingActionButton fabBtnCamera = findViewById(R.id.fab_menu_item_camera);
    com.github.clans.fab.FloatingActionButton fabBtnRecord = findViewById(R.id.fab_menu_item_record);
    com.github.clans.fab.FloatingActionButton fabBtnFreehand = findViewById(R.id.fab_menu_item_freehand);
    com.github.clans.fab.FloatingActionButton fabBtnText = findViewById(R.id.fab_menu_item_text);
    fabBtnCamera.setOnClickListener(this);
    fabBtnRecord.setOnClickListener(this);
    fabBtnFreehand.setOnClickListener(this);
    fabBtnText.setOnClickListener(this);

  }
//点击左上菜单图标调出菜单，会依次调用onOptionsItemSelected和onSupportNavigateUp函数
    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

  @Override
  public void onClick(View menuItem) {
    switch (menuItem.getId()) {
      case R.id.fab_menu_item_camera: {
        Intent intent = new Intent(MainActivity.this, NoteActivity.class);
        //intent.putExtra(INTENT_QUICK_ACTION, INTENT_QUICK_ACTION_CAMERA);
        startActivity(intent);
      }
      break;
      case R.id.fab_menu_item_record: {
        Intent intent = new Intent(MainActivity.this, NoteActivity.class);
        //intent.putExtra(INTENT_QUICK_ACTION, INTENT_QUICK_ACTION_RECORD);
        startActivity(intent);
      }
      break;
      case R.id.fab_menu_item_freehand: {
        Intent intent = new Intent(MainActivity.this, NoteActivity.class);
        //intent.putExtra(INTENT_QUICK_ACTION, INTENT_QUICK_ACTION_FREEHAND);
        startActivity(intent);
      }
      break;
      case R.id.fab_menu_item_text: {
        Intent intent = new Intent(MainActivity.this, NoteActivity.class);
          intent.putExtra("username", username);
        startActivity(intent);
      }
      break;
    }
    mFabMenu.hideMenu(true);
  }

  @Override
  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    return false;
  }
  public void ShareClick(View v){
      Intent intent=new Intent(Intent.ACTION_SEND);
      intent.setType("text/plain");
      intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
      intent.putExtra(Intent.EXTRA_TEXT, "欢迎下载笔记助手APP");
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(Intent.createChooser(intent, getTitle()));
  }
  public String getUsername(){

        return username;
    }
}
